<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Matiere_premiere_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    /*     * *************************************************************************
     * Accesseurs
     * ************************************************************************ */

    public function getList() {
        return $this->db
                        ->select('ARTICLE_ID, ARTICLE_REF, ARTICLE_LIBELLE, ARTICLE_SEUIL, UNITE_ID')
                        ->from('ARTICLE')
                        ->where('ARTICLE_MATIERE_PREMIERE', 1)
                        ->get()
                        ->result();
    }

    public function get($ARTICLE_ID) {
        $result = $this->db
                ->select('ARTICLE_ID, ARTICLE_REF, ARTICLE_LIBELLE, ARTICLE_SEUIL, UNITE_ID')
                ->from('ARTICLE')
                ->where('ARTICLE_ID', $ARTICLE_ID)
                ->get()
                ->result();

        if (count($result) == 1) {
            $result = $result[0];
        }

        return $result;
    }

    /*     * *************************************************************************
     * Méthodes d'enregistrement
     * ************************************************************************ */

    public function Create($ARTICLE_REF, $ARTICLE_LIBELLE, $ARTICLE_SEUIL, $UNITE_ID) {
        $this->db->set('ARTICLE_REF', $ARTICLE_REF)
                ->set('ARTICLE_LIBELLE', $ARTICLE_LIBELLE)
                ->set('ARTICLE_SEUIL', $ARTICLE_SEUIL)
                ->set('UNITE_ID', $UNITE_ID)
                ->set('ARTICLE_MATIERE_PREMIERE', 1)
                ->insert('ARTICLE');
        /*
          $ARTICLE_ID = $this->db->insert_id();

          $this->db->set('UNITE_ID', $UNITE_ID)
          ->set('ARTICLE_ID', $ARTICLE_ID)
          ->insert('matiere_premiere'); */
    }

    public function Update($ARTICLE_ID, $ARTICLE_REF, $ARTICLE_LIBELLE, $ARTICLE_SEUIL, $UNITE_ID) {
        $data = array(
            'ARTICLE_REF' => $ARTICLE_REF,
            'ARTICLE_LIBELLE' => $ARTICLE_LIBELLE,
            'ARTICLE_SEUIL' => $ARTICLE_SEUIL,
            'UNITE_ID' => $UNITE_ID
        );
        $this->db->where('ARTICLE_ID', $ARTICLE_ID)
                ->update('ARTICLE', $data);
        /*
          $data = array(
          'UNITE_ID' => $UNITE_ID
          );

          $this->db->where('ARTICLE_ID', $ARTICLE_ID)
          ->update('matiere_premiere', $data); */
    }

    public function Delete($ARTICLE_ID) {
        $this->db->where('ARTICLE_ID', $ARTICLE_ID)
                ->delete('ARTICLE');
    }

    /*     * *************************************************************************
     * Méthodes supplémentaires
     * ************************************************************************ */

    public function getList_Autocomplete($maxRows, $name_startsWith) {
        return $this->db
                        ->select('ARTICLE_ID, ARTICLE_REF, ARTICLE_LIBELLE')
                        ->from('ARTICLE')
                        ->like('ARTICLE_REF', $name_startsWith, 'after')
                        ->get()
                        ->result();
    }

    public function getAlerts() {
        $articles = $this->db
                ->select('ARTICLE_ID, ARTICLE_REF, ARTICLE_LIBELLE, ARTICLE_SEUIL')
                ->from('ARTICLE')
                ->where('ARTICLE_MATIERE_PREMIERE', true)
                ->get()
                ->result()
        ;

        foreach ($articles as $key => $art) {
            $articles[$key]->ARTICLE_SEUIL = (int) $articles[$key]->ARTICLE_SEUIL;
            $stock = $this->db
                    ->select('SUM(STOCK_QUANTITE) as STOCK')
                    ->from('STOCK')
                    ->where('ARTICLE_ID', $art->ARTICLE_ID)
                    ->get()
                    ->result();
            if (count($stock) > 0) {
                if ($stock[0]->STOCK == null)
                    $articles[$key]->STOCK = 0;
                else
                    $articles[$key]->STOCK = (int) $stock[0]->STOCK;
            }
            else
                $articles[$key]->STOCK = 0;

            if ($articles[$key]->STOCK >= $articles[$key]->ARTICLE_SEUIL)
                unset( $articles[$key]);
        }

        return $articles;
    }

}

?>
